package com.mifengkong.cygshare;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

import com.mifengkong.cygshare.base.BaseActivity;
import com.mifengkong.cygshare.bean.CreateOrderBean;
import com.mifengkong.cygshare.bean.KaMaoVipBean;
import com.mifengkong.cygshare.model.CreateOrderModel;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.statistic.alipay.PayManager;
import com.mifengkong.statistic.umengsignin.BaseUmLoginListener;
import com.mifengkong.statistic.umengsignin.WeChatSignInManager;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {

    private PayManager payManager;
    private Button btnPay;
    private RadioButton rbOne;
    private RadioButton rbTwo;
    private List<KaMaoVipBean> mList;
    private String vipId;

    private Button btnShowDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPay = findViewById(R.id.am_btn_gopay);
        rbOne = findViewById(R.id.am_rb_one);
        rbTwo = findViewById(R.id.am_rb_two);
        btnShowDialog = findViewById(R.id.am_btn_show_dialog);
        payManager = new PayManager(this);

        WeChatSignInManager.with(this).initWeChatLogin(new BaseUmLoginListener() {
            @Override
            public void onSuccess(SHARE_MEDIA platform, Map<String, String> dataMap) {

            }
        });

        initData();
        initListener();

    }

    @Override
    protected void initTitleBar() {
//        new FRTitleBar.FRTiTitleBarBuilder(this)
//                .setBackgroundColor(R.color.colorPrimary)
//                .setDefaultLeft()
//                .setRightContent("右边")
////                .setLeftOnClickListener(new View.OnClickListener() {
////                    @Override
////                    public void onClick(View v) {
////                        //不设置setLeftOnClickListener的话默认点击为返回事件
////                        FRToast.showToastSafe("点击了左边");
////                    }
////                })
//                .setRightOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        FRToast.showToastSafe("点击了右边");
//                    }
//                })
//                .builder();
    }

    private void initData() {
//        GetVipListModel.getInstance().execute(new FRBaseObserver<List<KaMaoVipBean>>() {
//            @Override
//            protected void onBaseNext(List<KaMaoVipBean> data) {
//                mList = data;
//                if (data.size() > 0) {
//                    rbOne.setText(data.get(0).getTitle());
//                }
//                if (data.size() > 1) {
//                    rbTwo.setText(data.get(1).getTitle());
//                }
//            }
//        });
    }

    private void initListener() {
        btnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rbOne.isChecked()) {
                    vipId = mList.get(0).getId();
                } else {
                    vipId = mList.get(1).getId();
                }
                if (FRString.isEmpty(vipId)) {
                    FRToast.showToastSafe("VipId不能为空");
                    return;
                }
                goPay();
            }
        });

//        btnShowDialog.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                showCommonDialog();
//                showMaterialDesignDialog();
//            }
//        });

        payManager.setOnAliPaySuccess(new PayManager.OnAliPaySuccess() {
            @Override
            public void paySuccess() {
                FRToast.showToastSafe("支付成功");
            }

            @Override
            public void payFailure() {
                FRToast.showToastSafe("支付失败");
            }
        });
    }

//    private void showCommonDialog() {
//        final FRDialog dialog = new FRDialog.Builder(this)
//                .setContentView(R.layout.dialog_check_update)
//                .setText(R.id.dcu_tv_cancel, "否")
//                .setText(R.id.dcu_tv_update, "是")
//                .setText(R.id.dcu_tv_title, "温馨提示")
//                .setText(R.id.dcu_tv_content, "1.文字文字我是文字文字文字我是文字文字文字我是文字！\n2.文字文字文字文字文字\n3.文字文字文字文字文字")
//                .setDefaultAnim()
//                .show();
//
//        dialog.setOnClickListener(R.id.dcu_tv_cancel, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//
//        dialog.setOnClickListener(R.id.dcu_tv_update, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(MainActivity.this, "点击了是", Toast.LENGTH_SHORT).show();
//            }
//        });
//    }
//
//    private void showMaterialDesignDialog() {
//        final FRDialog dialog = new FRDialog.Builder(this)
//                .materialDesign()
//                .setTitle("温馨提示")
//                .setMessage("1.文字文字我是文字文字文字我是文字文字文字我是文字！\n2.文字文字文字文字文字\n3.文字文字文字文字文字")
//                .setNegativeAndPositive("知道了")
//                .setNegativeAndPositiveTextColor(R.color.colorAccent)
//                .show();
//        dialog.setMaterialDesignNegativeListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                FRToast.showToastSafe("点击了知道了");
//            }
//        });
//    }

    private void goPay() {
        CreateOrderModel.getInstance().execute(vipId, new FRBaseObserver<CreateOrderBean>() {
            @Override
            protected void onBaseNext(CreateOrderBean data) {
                payManager.goToAliPay(data.getOrder_info());
            }
        });
    }
}